package com.cos.emp.web.controller;

import com.cos.emp.common.Const;
import com.cos.emp.entity.Emp;
import com.cos.emp.services.EmpServices;
import com.cos.emp.services.impl.EmpServicesImpl;
import com.cos.emp.util.DateUtils;
import com.cos.emp.util.StringUtils;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/6/1 14:30
 * @description:
 */
@WebServlet("/emp")
public class EmpController extends BaseServlet {

    private EmpServices es = new EmpServicesImpl();




    public String addEmp(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //获取用户提交数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //封装数据
        Emp e =new Emp() ;
        BeanUtils.populate(e,parameterMap);
        e.setBornDate(DateUtils.stringToDate((String)parameterMap.get("bornDates")[0]));

        //范文业务层
        boolean ans = es.addEmp(e);
        String msg = "新增失败";
        if (ans) {
            msg="新增成功";
        }
        request.setAttribute("msg", msg);
        return Const.FORWARD+"message.jsp";
    }
    public String delEmp(HttpServletRequest request, HttpServletResponse response) {
        //获取用户提交的数据
        Integer eno = StringUtils.stringToInt(request.getParameter("eno"));
        //访问业务操作
        boolean ans = es.delEmp(eno);
        String msg = "删除失败";
        if (ans) {
            msg="删除成功";
        }
        request.setAttribute("msg", msg);
        return Const.FORWARD+"message.jsp";
    }
    /**
     * @author: COS
     * @return:
     * @time: 2022/6/1 15:09
     * @description: 分页查询
     */
    public String getEmpByPages(HttpServletRequest request, HttpServletResponse response) {
        //1 页码
        String pageIndex = request.getParameter("pageIndex");
        //
        Integer pageNum = pageIndex == null ? 1 : Integer.valueOf(pageIndex);
        //访问业务层获取分页数据
        PageInfo<Emp> pi = es.getEmpByPages(pageNum, 5);
        request.setAttribute("pi", pi);
        return Const.FORWARD+"index.jsp";
    }

    public String doLogin(HttpServletRequest request, HttpServletResponse response) {
        //1
        String ename = request.getParameter("ename");
        String pwd = request.getParameter("pwd");
        //2 访问业务层 处理登录业务
        Emp e = es.doLogin(ename, pwd);
        //3 员工数据是否存在
        if (e != null) {
            request.getSession().setAttribute(Const.IS_LOGIN, e);
            return Const.REDIRECT+"emp?method=getEmpByPages";
//            return Const.REDIRECT + "index.jsp";
        } else {
            request.setAttribute("msg", "用户名或密码有误");
            return Const.FORWARD + "login.jsp";
        }
    }


}
